﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SHNOPData.SHNOPModel;
using SHNOPData.common;
using System.IO;
using HelperClass;
using SHNOPData.ImportExcel;
using System.Data;
using SHNOPData.ExportExcel;
public partial class Load : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ///导出网规信息
            string searchType = Request["searchType"];
            if (searchType != null)
            {
                ExportNtPlan(searchType);
            }
            ///导入excel
            string loadType = Request["loadType"];
            if (loadType != null)
            {
                Import(loadType);
            }
            ///下载模板
            string fileName = Request["templeteName"]; 
            if (fileName!=null)
            {
                DownLoad(fileName);
            }
            string guihuaID = Request["guihuaID"];
            string historyType = Request["historyType"];//0:站点，1：端口，2：频率，3：入网
            if (guihuaID != null && historyType != null)
            {
                string company = Server.UrlDecode(Request.Form["company"]);
                string _2gGuiHuaId = Request.Form["_2GGuihuaId"];
                string tdGuiHuaId = Request.Form["tdGuihuaId"]; 
                ExportHistory(guihuaID,company,_2gGuiHuaId,tdGuiHuaId, historyType);
            }

        }
    }
    /// <summary>
    /// 根据规划ID和类型导出历史信息
    /// </summary>
    /// <param name="guihuaID"></param>
    /// <param name="historyType">0:站点，1：端口，2：频率，3：入网</param>
    private void ExportHistory(string guihuaID, string company, string _2gGuiHuaId,string tdGuiHuaId, string historyType)
    {
        DataSet ds = null;
        if (HttpContext.Current != null && HttpContext.Current.Session["historySession"] != null)
        {
            ds = (DataSet)HttpContext.Current.Session["historySession"];
        }
        else
        {
            Guid gGuihuaID = new Guid(guihuaID);
            ds = AppDBMng.GetNTPlaning_HistoryList(gGuihuaID);
        }
        if (ds != null)
        {
            new ExportExcelFactory().ExportHistoryInfo(company,  _2gGuiHuaId, tdGuiHuaId,historyType, ds);
        }
        
    }
    /// <summary>
    /// 下载模板
    /// </summary>
    /// <param name="fileName"></param>
    private void DownLoad(string fileName)
    {
        //客户端保存的文件名
        string filePath = Server.MapPath("~") + "templet\\" + fileName;//路径
        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();
    }
    /// <summary>
    /// 导出网规信息
    /// </summary>
    /// <param name="type"></param>
    private void ExportNtPlan(string type)
    {
        new ExportExcelFactory().ExportNtPlanInfo(type);
    }
    /// <summary>
    /// 导入excel到数据库
    /// </summary>
    /// <param name="loadType"></param>
    private void Import(string loadType)
    {
        HttpPostedFile postFile = Request.Files[0];
        Stream str = postFile.InputStream;
        new ImportFactory().ImportStream(loadType, str);
    }
}